
package com.zuipin.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.OrderInvoiceInfo;
import com.zuipin.enums.InvoiceTitle;
import com.zuipin.enums.InvoiceType;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IOrderInvoiceInfoService;
import com.zuipin.util.RegexUtils;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.EnumVo;
import com.zuipin.vo.OrderInvoiceInfoVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 发票管理
 *
 * @ClassName OrderInvoiceInfoController
 * @author xujinding
 * @date 2017年5月9日 上午9:50:11
 */

@Controller
@RequestMapping(value = "/invoice")
@Api(value = "发票管理-xujinding", description = "发票管理-xujinding")
public class OrderInvoiceInfoController {
	
	@Resource
	private IOrderInvoiceInfoService orderInvoiceInfoservice;
	
	@RequestMapping(value = "/findInvoiceType", method = RequestMethod.POST)
	@ApiOperation(notes = "查询发票类型-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询发票类型-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findInvoiceType() {
		Result<List<EnumVo>> res = new Result();
		List<EnumVo> list = InvoiceType.findInvoiceType();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/findInvoiceTitle", method = RequestMethod.POST)
	@ApiOperation(notes = "查询发票抬头-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询发票抬头-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findInvoiceTitle() {
		Result<List<EnumVo>> res = new Result();
		List<EnumVo> list = InvoiceTitle.findInvoiceTitle();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/findOrderInvoiceList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询发票列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询发票列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "invoiceType", required = false, regex = "ELECTRONIC_INVOICE|ORDINARY_INVOICE|VTA_INVOICE"), @Valid(name = "invoiceTitle", required = false),
			@Valid(name = "invoiceStatus", required = false, regex = "YES|NO"), @Valid(name = "orderBy", required = false, regex = "asc|desc"),
			@Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "startTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "endTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<OrderInvoiceInfo>> findOrderInvoiceList(
			// 不开发票： NOT_INVOICE ，个人：PERSONAL，公司：COMPANY,
			@ApiParam(name = "invoiceType", value = "发票类型  电子发票:ELECTRONIC_INVOICE 普通：ORDINARY_INVOICE，增值：VTA_INVOICE", required = false) @RequestParam(required = false) String invoiceType,
			@ApiParam(name = "invoiceTitle", value = "发票抬头", required = false) @RequestParam(required = false) String invoiceTitle,
			@ApiParam(name = "orderNo", value = "订单号", required = false) @RequestParam(required = false) String orderNo,
			@ApiParam(name = "memberName", value = "收票人", required = false) @RequestParam(required = false) String memberName,
			@ApiParam(name = "startTime", value = "开始时间", required = false) @RequestParam(required = false) String startTime,
			@ApiParam(name = "endTime", value = "结束时间", required = false) @RequestParam(required = false) String endTime,
			@ApiParam(name = "sort", value = "排序字段名，，随出参", required = false) @RequestParam(required = false) String sort,
			@ApiParam(name = "orderBy", value = "asc desc", required = false) @RequestParam(required = false) String orderBy,
			@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "invoiceStatus", value = "发票状态 未开：NO，已开  ：YES", required = false) @RequestParam(required = false) String invoiceStatus,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		OrderInvoiceInfo invoice = new OrderInvoiceInfo();
		invoice.setInvoiceTitle(invoiceTitle);
		invoice.setOrderNo(orderNo);
		invoice.setInvoiceType(invoiceType);
		invoice.setMemberName(memberName);
		invoice.setSysId(sysId);
		invoice.setEndTime(endTime);
		invoice.setInvoiceStatus(invoiceStatus);
		invoice.setStartTime(startTime);
		PageResult<List<OrderInvoiceInfo>> pov = orderInvoiceInfoservice.findOrderInvoiceList(invoice, orderBy, sort, pageNumber, pageSize);
		return pov;
		
	}
	
	@RequestMapping(value = "/findOrderInvoiceById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询发票详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询发票详情-xujinding")
	@ResponseBody
	public Result<OrderInvoiceInfo> findOrderInvoiceById(//
			@ApiParam(name = "id", value = "发票id", required = true) @RequestParam(required = true) Long id,
			@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId) {
		OrderInvoiceInfo invoice = new OrderInvoiceInfo();
		invoice.setId(id);
		invoice.setSysId(sysId);
		Result<OrderInvoiceInfo> pov = orderInvoiceInfoservice.findOrderInvoiceById(invoice);
		return pov;
		
	}
	
	@Valids({ @Valid(name = "invoiceNo", required = true, maxLength = 1000) })
	@RequestMapping(value = "/modityInvoiveStatus", method = RequestMethod.POST)
	@ApiOperation(notes = "确认开票-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常"//
			+ "</br>2000-查无发票信息" + "</br>8000-修改状态失败", value = "确认开票-xujinding")
	@ResponseBody
	public Result modityInvoiveStatusById(//
			@ApiParam(name = "id", value = "发票id", required = true) @RequestParam(required = true) Long id,
			@ApiParam(name = "updateId", value = "修改人", required = true) @RequestParam(required = true) Long updateId,
			@ApiParam(name = "invoiceNo", value = "开票号码", required = true) @RequestParam(required = true) String invoiceNo,
			@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId) throws CustomException {
		Result res = new Result();
		if (!StringUtil.isBlank(invoiceNo)) {
			String tempNo = invoiceNo.replaceAll(",", "");
			if (!RegexUtils.isENG_NUM(tempNo)) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("开票号码只能是数字或字母");
				return res;
			}
		} else {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("请至少输入一个发票号码");
			return res;
		}
		OrderInvoiceInfo invoice = new OrderInvoiceInfo();
		invoice.setId(id);
		invoice.setInvoiceNo(invoiceNo);
		invoice.setSysId(sysId);
		invoice.setInvoiceStatus(OrderInvoiceInfo.STATUS_YES);
		invoice.setUpdateId(updateId);
		orderInvoiceInfoservice.modityInvoiveStatusById(invoice);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@Valids({ @Valid(name = "expressNo", required = true, regex = RegexUtils.INTEGER_NEGATIVE, maxLength = 50), @Valid(name = "expressCode", required = true, maxLength = 50) })
	@RequestMapping(value = "/modityExpressInvoiveInfo", method = RequestMethod.POST)
	@ApiOperation(notes = "填写快递信息-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常"//
			+ "</br>8001-只有在未寄出的时候才能填写快递信息" + "</br>2000-查无发票信息", value = "填写快递信息-xujinding")
	@ResponseBody
	public Result modityExpressInvoiveInfo(//
			@ApiParam(name = "id", value = "发票id", required = true) @RequestParam(required = true) Long id,
			@ApiParam(name = "updateId", value = "修改人", required = true) @RequestParam(required = true) Long updateId,
			@ApiParam(name = "expressNo", value = "快递单号", required = true) @RequestParam(required = true) String expressNo,
			@ApiParam(name = "expressCode", value = " 快递公司编码", required = true) @RequestParam(required = true) String expressCode,
			@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId) throws CustomException {
		Result res = new Result();
		OrderInvoiceInfo invoice = new OrderInvoiceInfo();
		invoice.setExpressCode(expressCode);
		invoice.setExpressNo(expressNo);
		invoice.setSysId(sysId);
		invoice.setUpdateId(updateId);
		invoice.setId(id);
		orderInvoiceInfoservice.modityExpressInvoiveInfo(invoice);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@Valids({ @Valid(name = "invoice.id", required = true), //
			@Valid(name = "invoice.updateId", required = true), //
			@Valid(name = "invoice.taxpayersNo", required = false, regex = RegexUtils.taxpayersNo), //
			@Valid(name = "invoice.registerMobilePhone", required = false, regex = RegexUtils.registerMobilePhone), //
			@Valid(name = "invoice.openAccount", required = false, regex = RegexUtils.openAccount), //
			@Valid(name = "invoice.email", required = false, regex = RegexUtils.EMAIL), //
			@Valid(name = "invoice.invoiceTitle", required = false, regex = "PERSONAL|COMPANY"), //
			@Valid(name = "invoice.invoiceType", required = true, regex = "ELECTRONIC_INVOICE|ORDINARY_INVOICE|VTA_INVOICE") })
	@RequestMapping(value = "/modityInvoiveInfo", method = RequestMethod.POST)
	@ApiOperation(notes = "修改发票信息-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常"//
			+ "</br>2000-查无发票信息", value = "修改发票信息-xujinding")
	@ResponseBody
	public Result modityInvoiveInfo(@RequestBody @ApiParam(value = "修改发票信息") OrderInvoiceInfoVo invoice) throws CustomException {
		Result res = new Result();
		orderInvoiceInfoservice.modityInvoiveInfo(invoice);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/findInvoiveInfoByIds", method = RequestMethod.POST)
	@ApiOperation(notes = "根据多个发票Id查询-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "根据多个发票Id查询-xujinding")
	@ResponseBody
	public Result<List<OrderInvoiceInfo>> findInvoiveInfoByIds(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "ids", value = "发票id(多个)", required = true) @RequestParam(required = true) String ids) throws CustomException {
		Result<List<OrderInvoiceInfo>> res = new Result<>();
		List<OrderInvoiceInfo> invoiveInfoList = orderInvoiceInfoservice.findInvoiveInfoByIds(sysId, ids);
		res.setData(invoiveInfoList);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/findStatusNum", method = RequestMethod.POST)
	@ApiOperation(notes = "查询发票状态个数-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>key:状态，，值数量", value = "查询发票状态个数-xujinding")
	@ResponseBody
	public Result<Map<String, Integer>> findStatusNum(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId) throws CustomException {
		Result<Map<String, Integer>> res = new Result<>();
		Map<String, Integer> map = orderInvoiceInfoservice.findStatusNum(sysId);
		res.setData(map);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
}
